use std::error::Error;
use std::fmt;
use std::fs;
use std::str::FromStr;

fn read_numbers<T>(file_name: &str) -> Vec<T>
where
    T: FromStr,
    <T as FromStr>::Err: fmt::Debug, // must add this trait bound for unwrap!
{
    let contents = fs::read_to_string(file_name).unwrap();
    let numbers = contents
        .trim()
        .replace("\r", "")
        .split("\n")
        .map(|x| x.parse::<T>().unwrap())
        .collect::<Vec<_>>();
    return numbers;
}

fn part1() {
    let nums = read_numbers::<i32>("input.txt");
    //println!("{:?}", nums);
    for (i, ei) in nums.iter().enumerate() {
        for (j, ej) in nums.iter().enumerate() {
            if i != j && ei + ej == 2020 {
                println!("{}", ei * ej);
                return;
            }
        }
    }
}

fn part2() {
    let nums = read_numbers::<i32>("input.txt");
    //println!("{:?}", nums);
    for (i, ei) in nums.iter().enumerate() {
        for (j, ej) in nums.iter().enumerate() {
            for (k, ek) in nums.iter().enumerate() {
                if i != j && j != k && i != k && ei + ej + ek == 2020 {
                    println!("{}", ei * ej * ek);
                    return;
                }
            }
        }
    }
}

fn main() {
    part1();
    part2();
}
